import { getZkxmxxList } from "@api"
import { XmglAddEmits, XmglAddProps } from "./prop"
import { EventSure } from "@ui/utils/var"
import { TableInstance } from "element-plus"
import { SetupContext } from "vue"

export function use(props: XmglAddProps, emit: SetupContext<XmglAddEmits>['emit']): any {
  const table = ref<TableInstance>(),
    tableData = ref<any[]>([]),
    addFormData = ref({ jsm: '', xmlb: '', xjbz: '', yqid: props.yqid || '' })
  watch(() => props.yqid, (v) => {
    addFormData.value.yqid = v || ''
    query()
  })
  query()
  function query() {
    const { jsm, yqid } = addFormData.value
    getZkxmxxList({ yqid, jsm }).then(({ data: { list } }) => {
      tableData.value = list
    })
  }
  function addRest() {
    addFormData.value = { yqid: props.yqid } as any
  }
  function save() {
    if (table.value) {
      emit(EventSure, table.value.getSelectionRows())
    }
  }
  function clear() {
    if (table.value)
      table.value.clearSelection()
  }
  return { query, save, addFormData, addRest, tableData, table, clear }
}